using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace Report.PhieuD
{
    public partial class UcD101 : DevExpress.XtraEditors.XtraUserControl
    {
        public UcD101()
        {
            InitializeComponent();
        }

        private void UcD101_Load(object sender, EventArgs e)
        {
            DataTable dtD101 = new DsReportX.Table7ColDataTable();
            dtD101 = CalculateData(dtD101);
            gridControlA.DataSource = dtD101;
        }

        DataTable CalculateData(DataTable dt)
        {
            SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
            SqlCommand cmd = con.CreateCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            try
            {
                con.Open();
                cmd.CommandText = "SELECT ChiTra2011 Number1, CONVERT(MONEY, 100.0 * ChiTra2011/TongChiTra2011) Number2, \n"+
                                "ChiTra2012 Number3 , CONVERT(MONEY, 100.0 * ChiTra2012/TongChiTra2012) Number4 \n" +
                                "FROM ( \n"+
                                " SELECT SUM(Number1) ChiTra2011,  SUM(Number2) ChiTra2012 \n"+
                                " FROM Phieu_DangBang A INNER JOIN Phieu B ON A.PhieuID = B.ID  \n"+ 
                                " WHERE B.LoaiPhieu = 'PhieuD' AND MaCau = 13  \n"+
                                " GROUP BY ThuTuTrongBang \n"+
                                " UNION ALL \n"+
                                " SELECT SUM(Number1) TongChiTra2011,  SUM(Number2) TongChiTra2012 \n"+
                                " FROM Phieu_DangBang A INNER JOIN Phieu B ON A.PhieuID = B.ID  \n"+
                                " WHERE B.LoaiPhieu = 'PhieuD' AND MaCau = 13 AND ThuTuTrongBang IN (0,2,4) \n"+
                                ") ChiTra, ( \n"+
                                " SELECT SUM(Number1) TongChiTra2011,  SUM(Number2) TongChiTra2012 \n"+
                                " FROM Phieu_DangBang A INNER JOIN Phieu B ON A.PhieuID = B.ID  \n"+
                                " WHERE B.LoaiPhieu = 'PhieuD' AND MaCau = 13  AND ThuTuTrongBang IN (0,2,4) \n"+
                                ") TongChiTra  ";

                da.SelectCommand = cmd;
                da.Fill(dt);

                return dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
            finally
            {
                con.Close();
            }
        }
    }
}
